package com.ipet.app.service.dao;

/**
 * Created by jindongping on 16/3/16.
 */

import android.annotation.TargetApi;
import android.content.ContentValues;
import android.os.Build;

import com.ipet.app.util.KLog;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * DbHelper 数据库操作类
 * 使用该类操作数据库 new DbHelper<XXX>().操作...
 * @author cuiran
 * @version 1.0.0
 */
public class DbHelper<T> {
    private static final String TAG = "DbHelper";

    /**
     * 新增一条记录
     */
    public int create(T po) {
        SQLiteHelperOrm db = new SQLiteHelperOrm();
        try {
            Dao dao = db.getDao(po.getClass());
            return dao.create(po);
        } catch (SQLException e) {
            KLog.e(TAG, "create", e);
        } finally {
            if (db != null)
                db.close();
        }
        return -1;
    }

    /**
     * 新增一条记录
     */
    public Dao.CreateOrUpdateStatus createOrUpdate(T po) {
        SQLiteHelperOrm db = new SQLiteHelperOrm();
        try {
            Dao dao = db.getDao(po.getClass());
            return dao.createOrUpdate(po);
        } catch (SQLException e) {
            KLog.e(TAG, "create", e);
        } finally {
            if (db != null)
                db.close();
        }
        return null;
    }

    public boolean exists(T po, Map<String, Object> where) {
        SQLiteHelperOrm db = new SQLiteHelperOrm();
        try {
            Dao dao = db.getDao(po.getClass());
            if (dao.queryForFieldValues(where).size() > 0) {
                return true;
            }
        } catch (SQLException e) {
            KLog.e(TAG, "exists", e);
        } finally {
            if (db != null)
                db.close();
        }
        return false;
    }

    public int createIfNotExists(T po, Map<String, Object> where) {
        SQLiteHelperOrm db = new SQLiteHelperOrm();
        try {
            Dao dao = db.getDao(po.getClass());
            if (dao.queryForFieldValues(where).size() < 1) {
                return dao.create(po);
            }
        } catch (SQLException e) {
            KLog.e(TAG, "createIfNotExists", e);
        } finally {
            if (db != null)
                db.close();
        }
        return -1;
    }

    /**
     * 查询一条记录
     */
    public List<T> queryForEq(Class<T> c, String fieldName, Object value) {
        SQLiteHelperOrm db = new SQLiteHelperOrm();
        try {
            Dao dao = db.getDao(c);
            return dao.queryForEq(fieldName, value);
        } catch (SQLException e) {
            KLog.e(TAG, "queryForEq", e);
        } finally {
            if (db != null)
                db.close();
        }
        return new ArrayList<T>();
    }

    /**
     * 删除一条记录
     */
    public int remove(T po) {
        SQLiteHelperOrm db = new SQLiteHelperOrm();
        try {
            Dao dao = db.getDao(po.getClass());
            return dao.delete(po);
        } catch (SQLException e) {
            KLog.e(TAG, "remove", e);
        } finally {
            if (db != null)
                db.close();
        }
        return -1;
    }

    /**
     * 根据特定条件更新特定字段
     *
     * @param c
     * @param values
     * @param columnName where字段
     * @param value      where值
     * @return
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public int update(Class<T> c, ContentValues values, String columnName, Object value) {
        SQLiteHelperOrm db = new SQLiteHelperOrm();
        try {
            Dao dao = db.getDao(c);
            UpdateBuilder<T, Long> updateBuilder = dao.updateBuilder();
            updateBuilder.where().eq(columnName, value);
            for (String key : values.keySet()) {
                updateBuilder.updateColumnValue(key, values.get(key));
            }
            return updateBuilder.update();
        } catch (SQLException e) {
            KLog.e(TAG, "update", e);
        } finally {
            if (db != null)
                db.close();
        }
        return -1;
    }

    /**
     * 更新一条记录
     */
    public int update(T po) {
        SQLiteHelperOrm db = new SQLiteHelperOrm();
        try {

            Dao dao = db.getDao(po.getClass());
            return dao.update(po);
        } catch (SQLException e) {
            KLog.e(TAG, "update", e);
        } finally {
            if (db != null)
                db.close();
        }
        return -1;
    }

    /**
     * 查询所有记录
     */
    public List<T> queryForAll(Class<T> c) {
        SQLiteHelperOrm db = new SQLiteHelperOrm();
        try {
            Dao dao = db.getDao(c);
            return dao.queryForAll();
        } catch (SQLException e) {
            KLog.e(TAG, "queryForAll", e);
        } finally {
            if (db != null)
                db.close();
        }
        return new ArrayList<T>();
    }

}